package com.rla.mqListener;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.rla.common.utils.redis.PubChannelPool;
import com.rla.common.utils.redis.QueueUtils;
import com.rla.framework.manager.AsyncManager;
import com.rla.service.IMallOrderService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * 异常随访处理
 */
@RequiredArgsConstructor
@Component
@Slf4j
public class PsiRedisPushPopListener {

    private final IMallOrderService orderService;

    /**
     * 自动完成订单
     */
    public void autoComplete() {
        String queueName = PubChannelPool.PSI_AUTO_COMPLETE_MILLISECOND;
        log.info("通道自动自动完成订单: {}, autoComplete", queueName);
        QueueUtils.subscribeBlockingQueue(queueName, (String orderId) -> {
            if (ObjectUtil.isEmpty(orderId)) {
                log.error("通道: {}, 查单队列=空异常不处理===订单号：{}", queueName, orderId);
                return;
            }
            AsyncManager.me().runTask(() -> {
                log.info("通道: {},  自动完成订单===订单号：{}", queueName, orderId);
                try {
                    orderService.autoComplete(orderId,false);
                } catch (Exception e) {
                    log.error("通道:{} 自动完成订单autoCancel,异常:{}", queueName, e.getMessage(), e);
                }
            });

        });
    }





}
